/**
 * 前台展示子路由 home
 */
const Router = require('koa-router');
const { getAllJSONByCondition, getJSONByCondition, updateViewsJsonById } = require('../utils/JsonFile')


// home子路由
let home = new Router();
home.get('/index', async (ctx) => {
    ctx.body = "Home index";
}).get('/getArticleById/:id', async (ctx) => {
    const { id } = ctx.params;
    console.log(id)
    try {
        let result = await updateViewsJsonById('article.json', id);
        ctx.body = result;
    } catch (e) {
        ctx.body = {
            code: 500,
            msg: '服务器错误'
        }
    }
}).get('/getHotArticleList', async (ctx) => {
    try {
        let result = await getAllJSONByCondition('article.json', {isHot: true}, 3);
        ctx.body = result;
    } catch (e) {
        ctx.body = {
            code: 500,
            msg: '服务器错误'
        }
    }
}).get('/getNewArticleList', async (ctx) => {
    try {
        let result = await getAllJSONByCondition('article.json', {}, 3);
        ctx.body = result;
    } catch (e) {
        ctx.body = {
            code: 500,
            msg: '服务器错误'
        }
    }
}).get('/getArticleList', async (ctx) => {
    let {currPage=1, pageSize=5} = ctx.query;
    try {
        let result = await getJSONByCondition('article.json',{isPublish: true},  currPage, pageSize);
        ctx.body = result;
    } catch (e) {
        ctx.body = {
            code: 500,
            msg: '服务器错误'
        }
    }
});


module.exports = home;
